# coding: utf-8

#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can stretch IK joint
#Note: can maintain volume on and off.
#FN: Ik_stretch(ikhandle ,keepVolum )
#           拉伸（ikhandle，缩放方式，保持体积）
#Date: 2012/08/01_v1.0

import maya.cmds as mc
import rosa_ikStretchNodeCreate as rsnc
reload(rsnc)
import RosaCommands as rc

def Ik_stretch(prefix , ikhandle , span ,vectorCtr=None):

        #get both joint and create distance tool
        ikjntlist = rc.get_ikEffortJnt(ikhandle)
        stretchNode = rsnc.ikStretchNode( prefix ,span , vectorCtr)

 #-------------------------------------------------------------------------------------------------------------------------------------------------

        if span == 2:

                firstPointDis = mc.getAttr( ikjntlist[1]+'.translateX' )
                secoundPointDis = mc.getAttr( ikjntlist[2]+'.translateX' )

                mc.setAttr( stretchNode[0]+'.org_first_jointDis' , firstPointDis )
                mc.setAttr( stretchNode[0]+'.org_secound_jointDis' , secoundPointDis )

                mc.select(ikhandle)
                rc.makeObjZero('group','editGrp','On')

                mc.pointConstraint( ikjntlist[0] , stretchNode[1] , mo=False)
                mc.pointConstraint( ikhandle+'_editGrp' , stretchNode[2] , mo=False)

                mc.connectAttr(stretchNode[0]+'.real_first_jointDis' , ikjntlist[1]+'.translateX' )
                mc.connectAttr(stretchNode[0]+'.real_secound_jointDis' , ikjntlist[2]+'.translateX' )

                pointLocGrp = mc.createNode('transform' , n = prefix+'_pointLoc_Grp')
                mc.parent(stretchNode[1] , pointLocGrp)
                mc.parent(stretchNode[2] , pointLocGrp)

                rc.lockhideAttr(stretchNode[3] , 1 ,'org_first_jointDis' , 'org_secound_jointDis' , 'org_thirty_jointDis')

 #-------------------------------------------------------------------------------------------------------------------------------------------------


        elif span == 3:
                
                firstPointDis = mc.getAttr( ikjntlist[1]+'.translateX' )
                secoundPointDis = mc.getAttr( ikjntlist[2]+'.translateX' )
                thirtyPointDis = mc.getAttr( ikjntlist[3]+'.translateX' )

                mc.setAttr(stretchNode[0]+'.org_first_jointDis' , firstPointDis )
                mc.setAttr(stretchNode[0]+'.org_secound_jointDis' , secoundPointDis )
                mc.setAttr(stretchNode[0]+'.org_thirty_jointDis' , thirtyPointDis )

                mc.select(ikhandle)
                rc.makeObjZero('group','editGrp','On')

                mc.pointConstraint( ikjntlist[0] , stretchNode[1] , mo=False)
                mc.pointConstraint( ikhandle+'_editGrp'  , stretchNode[2] , mo=False)

                mc.connectAttr(stretchNode[0]+'.real_first_jointDis' , ikjntlist[1]+'.translateX')
                mc.connectAttr(stretchNode[0]+'.real_secound_jointDis' ,ikjntlist[2]+'.translateX' )
                mc.connectAttr(stretchNode[0]+'.real_thirty_jointDis' , ikjntlist[3]+'.translateX' )  

                pointLocGrp = mc.createNode('transform' , n = prefix+'_pointLoc_Grp')
                mc.parent(stretchNode[1] , pointLocGrp)
                mc.parent(stretchNode[2] , pointLocGrp)

                rc.lockhideAttr(stretchNode[3] , 1 ,'org_first_jointDis' , 'org_secound_jointDis' , 'org_thirty_jointDis')
        
 #--------------------------------------------------------------------------------------------------------------------------------------------------
                
        return stretchNode ,pointLocGrp